const app = getApp()
const $api = require('../../../utils/api.js').API;

Page({
    data: {
        // latitude:'38.9375',
        // longitude:'117.3571',
        // markers: [{
        //   id: 1,
        //   latitude: '38.9375',
        //   longitude: '117.3571',
        // }],
        avatar:'',
        img1:'/static/img/pub.png',
        submittrue:true,
        uploadtrue:false,
        form:{
        },
        type:1,
        imgUrl:app.globalData.imgUrl,
        imgsShow:true,
        region: ['天津市', '天津市', '全部'],
        money:['面议','5k以下','5k~8k','8k~10k','10k以上'],
        city:''
    },
    onLoad: function(t) {
        this.setData({
          avatar:wx.getStorageSync('avatarUrl')
        })
        this.getCompany();
    },
    submit:function(e){
      var key = e.target.dataset.key;
      if(key == 1){
        var sourceType = 'camera'
      }
      if(key == 2){
        var sourceType = 'album'
      }
      var that = this
      if(this.data.type == 1){
        var count = 1;
      }else{
        var count = 9;
      }
      wx.chooseImage({
        count: count, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: [sourceType], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res){
          that.imgUploadTwo(res.tempFilePaths)
        }
      })
    },
    imgUploadTwo:function(file){
      var that = this
      // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
      wx.uploadFile({
        url: that.data.imgUrl+'/api/img/upload',//此处换上你的接口地址
        filePath: file[0],
        name: 'img',
        header: {  
          "Content-Type": "multipart/form-data",
          'accept': 'application/json',  //若有token，此处换上你的token，没有的话省略
          'token':'tymy'
        },
        formData:{
          'type':that.data.type  //其他额外的formdata，可不写
        },
        success: function(res){
            if(that.data.type == 1){
              var form = that.data.form;
              form.license = res.data
              that.setData({
                img1:that.data.imgUrl+res.data,
                form:form,
                uploadtrue:false
              })
            }
            //机构配图
            if(that.data.type == 2){
              var form = that.data.form;
              form.imgs.push(res.data)
              that.setData({
                form:form,
                uploadtrue:false
              })
              if(form.imgs.length >= 5){
                that.setData({
                  imgsShow:false
                })
              }
            }
        }
      })
    },
    bindKeyInput:function(e){
      var form = this.data.form
      var name = e.target.dataset.name
      var value = e.detail.value
      form[name] = value
      this.setData({
        form:form
      })
    },
    submit3:function(){
      this.setData({
        uploadtrue:false
      })
    },
    imgupload:function(e){
      this.setData({
        uploadtrue:true,
        type:e.target.dataset.type
    })
    },
    delete:function(e){
      var index = e.target.dataset.id
      var form = this.data.form
      form.imgs.splice(index,1)
      if(form.imgs.length >= 5){
        this.setData({
          imgsShow:false
        })
      }else{
        this.setData({
          imgsShow:true
        })
      }
      this.setData({
        form:form
      })
    },
    companyAdd:function(){
      var that = this
      var form = this.data.form
      form.city = this.data.city
      var message = {'name':'机构名称必填',
                    'contacts':'联系人必填',
                    'phone':'联系电话必填',
                    'job':'招聘岗位必填',
                    'num':'招聘人数必填',
                    'address':'地址必填',
                    'license':'营业执照必须上传',
                    'imgs':'店铺照片必须上传',
                    }
      for (var x in message){
        if(!form[x]){
            wx.showToast({
              title:message[x],
              duration:2000,
              mask:true, 
              icon:'none',
            })
            return;
          }
      }
      that.setData({
        submittrue:false
      })
      $api.put('user/companyEdit',form).then(res => {
        //请求成功
        if(res.data.code == 1){
          wx.showToast({
            title: '修改成功!!',//提示文字
            duration:2000,//显示时长
            mask:true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon:'none', //图标，支持"success"、"loading"   
            success:function(){
              setTimeout(function(){
                wx.switchTab({
                  url: '/pages/user/index'
                })
              },2000) //延迟时间 这里是
            }
          })
        }
      })
    },
    bindRegionChange: function (e) {
      let region = e.detail.value
      if(e.detail.value[1] !== '全部'){
        var city = e.detail.value[1]
      }else{
        var city = e.detail.value[0]
      }
      let form = this.data.form
      form.city = city
      form.region = region.join(',');
      this.setData({
        region:region,
        form: form
    })
  },
  bindSelectChange: function(e){
    let name = e.target.dataset.name
    let dataCenter = this.data[name];
    let key = e.detail.value
    let form = this.data.form
    form[name] = key
    this.setData({
        form:form
    })
  },
  getCompany:function(){
    var that = this
    var data ={
      uid:wx.getStorageSync('uid')
    }
    $api.get('company/companyInfo',data).then(res =>{
      //请求成功
      if(res.data.code == 1){
        if(res.data.data.imgs){
          if(res.data.data.imgs.length >= 5){
            that.setData({
              imgsShow:false
            })
          }else{
            that.setData({
              imgsShow:true
            })
          }
        }else{
          res.data.data.imgs = []
          that.setData({
            imgsShow:true
          })
        }
        that.setData({
          form:res.data.data,
          city:res.data.data.city
        })
        // if(!res.data.data.latitude){
        //   that.getLocate()
        // }else{
        //   var markers = that.data.markers
        //   markers[0].latitude = res.data.data.latitude
        //   markers[0].longitude = res.data.data.longitude
        //   that.setData({
        //     latitude:res.data.data.latitude,
        //     longitude:res.data.data.longitude,
        //     markers:markers
        //   })
        // }

      }
    })
  },
  onShareAppMessage: function() {
    return {
        title: "天意美业直通车",
        path: "/pages/index/index?puid="+wx.getStorageSync('uid')
    };
  },
  //获取定位
  getLocate:function(){
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        var markers = that.data.markers
        markers[0].latitude = res.latitude
        markers[0].longitude = res.longitude
        that.setData({
          latitude:res.latitude,
          longitude:res.longitude,
          markers:markers
        })
      }
     })
  },
  chooseView:function(){
    var that = this
    wx.chooseLocation({
      success: function(res) {
        var markers = that.data.markers
        markers[0].latitude = res.latitude
        markers[0].longitude = res.longitude
        var form = that.data.form
        form.address = res.address
        form.latitude = res.latitude
        form.longitude = res.longitude
        that.setData({
          latitude:res.latitude,
          longitude:res.longitude,
          markers:markers,
          form:form
        })
      },
    })
  },
  goCenter:function(){
    wx.setStorageSync('formCity',this.data.form)
    wx.navigateTo({
      url: '/pages/selectCity/index'
    })
  },
  goJob:function(){
    wx.navigateTo({
      url: '/pages/login/post/index'
    })
  }
});